package com.jh3502.vivo.function;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.view.TimePickerView;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.function.author.AuthorListActivity;
import com.jh3502.vivo.function.contact.ContactFragment;
import com.jh3502.vivo.function.dialog.DialogMain;
import com.jh3502.vivo.function.main.MainFragment;
import com.jh3502.vivo.function.me.MeFragment;
import com.jh3502.vivo.function.message.MessageFragment;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.NoScrollViewPager;
import com.jh3502.vivo.view.SnackbarUtil;
import com.jh3502.vivo.view.TabMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import q.rorbin.badgeview.QBadgeView;

/**
 * author:harisucici on 2018/11/6 15:03
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class MainActivity extends BaseActivity<MainPresenter.MainView, MainPresenter> implements MainPresenter.MainView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.main_viewpager)
    NoScrollViewPager mainViewpager;
    @BindView(R.id.main_tab)
    TabMenu mainTab;
    @BindView(R.id.main_bottom_radio)
    RadioGroup mainBottomRadio;
    List<QBadgeView> qBadgeViews = new ArrayList<>(2);
    private MyAdapter adapter;
    private List<Fragment> fragments;// used for ViewPager adapter
    private int position;
    private boolean isEdit = false;
    private MessageFragment messageFragment;
    private TimePickerView timePickerView;


    @Override
    public MainPresenter initPresenter() {
        return new MainPresenter();
    }


    @Override
    protected int setViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("首页");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mPresenter.setMainView(this);

        mainTab.enableItemShiftingMode(false);
        mainTab.enableShiftingMode(false);
        mainTab.enableAnimation(false);
        if (checkPermission()) {
        } else {
            requestPermission();
        }




    }

    @Override
    protected void initData() {
        fragments = new ArrayList<>(4);
        BaseFragment mainFragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", "");
        mainFragment.setArguments(bundle);

        messageFragment = new MessageFragment();
        bundle = new Bundle();
        bundle.putString("title", "");
        messageFragment.setArguments(bundle);

        BaseFragment contactFragment = new ContactFragment();
        bundle = new Bundle();
        bundle.putString("title", "");
        bundle.putBoolean("hidebutton", true);
        contactFragment.setArguments(bundle);

        BaseFragment meFragment = new MeFragment();
        bundle = new Bundle();
        bundle.putString("title", "");
        meFragment.setArguments(bundle);

        fragments.add(mainFragment);
        fragments.add(messageFragment);
        fragments.add(contactFragment);
        fragments.add(meFragment);

        adapter = new MyAdapter(getSupportFragmentManager(), fragments);
        mainViewpager.setAdapter(adapter);
        initEvent();

        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", SpHelper.getInstance().getUid());
        mPresenter.toGetUser(body);
//        dismissLoadingDialog();
        mPresenter.toFindUnreadSum();

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg, SnackbarUtil.RED);

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (position == 0) {
            getMenuInflater().inflate(R.menu.menu_add, menu);
        } else if (position == 1) {
            if (isEdit) {
                getMenuInflater().inflate(R.menu.menu_done, menu);
            } else {
                getMenuInflater().inflate(R.menu.menu_edit, menu);
            }

        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_edit:
                isEdit = true;
                setSupportActionBar(toolbar);
                messageFragment.setEdit(isEdit);
                break;
            case R.id.action_done:
                isEdit = false;
                setSupportActionBar(toolbar);
                messageFragment.setEdit(isEdit);
                break;
            default:
        }
        return true;
    }

    private void initEvent() {
        mainTab.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            private int previousPosition = -1;

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                position = 0;
                switch (item.getItemId()) {
                    case R.id.tab_main:
                        position = 0;
                        toolbarTitle.setText("首页");
                        mainBottomRadio.check(R.id.radioButton1);
                        break;
                    case R.id.tab_msg:
                        position = 1;
                        hideRedPoint(position);
                        toolbarTitle.setText("消息");
                        mainBottomRadio.check(R.id.radioButton2);
                        break;
                    case R.id.tab_constact:
                        position = 2;
                        toolbarTitle.setText("人员列表");
                        mainBottomRadio.check(R.id.radioButton4);
                        break;
                    case R.id.tab_me:
                        position = 3;
                        toolbarTitle.setText("我们");
                        mainBottomRadio.check(R.id.radioButton5);
                        break;
                    case R.id.tab_empty: {
                        return false;
                    }
                    default:
                }
                if (previousPosition != position) {
                    mainViewpager.setCurrentItem(position, false);
                    previousPosition = position;
                }
                setSupportActionBar(toolbar);
                return true;
            }
        });

        mainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position >= 2) {
                    position++;
                }
                mainTab.setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @OnClick(R.id.main_home)
    public void onMainHomeClicked() {
//        Utils.getInstance().startActivity(this, AuthorListActivity.class);
        Utils.getInstance().startActivity(this, DialogMain.class);
//        hideRedPoint(0);
    }

    @Override
    public void getUserSuccess() {
        showShortToast("获取用户信息成功");

    }

    @Override
    public void findUnreadSumSuccess(Map<String, Object> map) {

        initRedPoint();
        if (!"0".equals(map.get("num"))) {
            addRedPoint(1);
        }
    }

    private void initRedPoint() {
        for (int i = 0; i < 5; i++) {
            QBadgeView qBadgeView = new QBadgeView(this);
            qBadgeView.setBadgeBackground(ContextCompat.getDrawable(this, R.drawable.point_red))
                    .setShowShadow(true).setBadgeNumber(-1)
                    .setGravityOffset(15, 8, true);
            qBadgeViews.add(qBadgeView);
        }
    }

    // add  place
    private void addRedPoint(int num) {
        qBadgeViews.get(num).bindTarget(mainTab.getBottomNavigationItemView(num));
    }

    //remove place
    private void hideRedPoint(int num) {
        qBadgeViews.get(num).hide(false);
    }

    private static class MyAdapter extends FragmentPagerAdapter {
        private List<Fragment> data;
        public MyAdapter(FragmentManager fm, List<Fragment> data) {
            super(fm);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }
    }


}
